package cn.gjculture.shopping.api.controller;

import cn.gjculture.shopping.api.entity.Customer;
import cn.gjculture.shopping.api.entity.PageInfo;
import cn.gjculture.shopping.api.entity.Pay;
import cn.gjculture.shopping.api.entity.Return;
import cn.gjculture.shopping.api.service.InterfaceService;
import cn.gjculture.shopping.api.service.PayService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/pay")
public class PayController {

    @Value("${pay.appSuccessUrl}")
    private String appSuccessUrl;

    @Value("${pay.unifySuccessUrl}")
    private String unifySuccessUrl;


    @Autowired
    InterfaceService interfaceService;


    @Autowired
    PayService payService;


    @ApiOperation(value = "第三方支付接口异步通知接口", notes = "第三方支付接口异步通知接口")
    @PostMapping("/back")
    @ResponseBody
    public String thirdBack(String code,String msg,String responseType,String responseParameters,String sign){
        JSONObject ret=new JSONObject();
        try {
            interfaceService.thirdBack(code,msg,responseType,responseParameters,sign);
        } catch (Exception e) {
            log.error("回调验证签名错误，msg:{}",e.getMessage());
            e.printStackTrace();
        }
        ret.put("code","000000");
        ret.put("msg","成功");
        return ret.toJSONString();
    }

    @ApiOperation(value = "第三方支付接口同步返回接口")
    @PostMapping("/payback")
    public String payBack(String code,String msg,String responseParameters,String sign) throws Exception {
        log.info("code:{}",code);
        log.info("msg:{}",msg);
        if(interfaceService.verifySign(code,msg,responseParameters,sign)){
            throw  new RuntimeException("verify sign fail");
        }
        return "redirect:"+appSuccessUrl+"?code="+code+"&msg="+msg;
    }

    @ApiOperation(value = "第三方支付接口同步返回接口")
    @PostMapping("/unifyBack")
    public String unifyPayBack(String code,String msg,String responseParameters,String sign) throws Exception {
        log.info("code:{}",code);
        log.info("msg:{}",msg);
        if(interfaceService.verifySign(code,msg,responseParameters,sign)){
            throw  new RuntimeException("verify sign fail");
        }
        return "redirect:"+appSuccessUrl+"?code="+code+"&msg="+msg;
    }

    @GetMapping(value = "/byCustomer")
    @ResponseBody
    public Return<List<Pay>> queryByCustomer(Customer customer,PageInfo pageInfo){
        Return ret=new Return();
        List<Pay> pays=payService.queryByCustomer(customer);
        if(!CollectionUtils.isEmpty(pays)){
            ret.getCommon().setRet(true);
            ret.setData(pays);
        }

        return ret;
    }
    @GetMapping(value = "/subAccountByCustomerNo")
    @ResponseBody
    public Return<List<Pay>> querySubAccountByCustomerNo(String customerNo, String consumerNo,Integer come, PageInfo pageInfo){
        Return ret=new Return();
        List<Map> subAccountByCustomerNo=payService.querySubAccountByCustomerNo(customerNo,consumerNo,come);
        if(!CollectionUtils.isEmpty(subAccountByCustomerNo)){
            ret.getCommon().setRet(true);
        }
        ret.setData(subAccountByCustomerNo);
        return ret;
    }
    @GetMapping(value = "/withdraw")
    @ResponseBody
    public Return queryWithdraw(Long customerNo,PageInfo info){
        Return ret=new Return();
        Map map=payService.queryWithdraw(customerNo);
        ret.setData(map);
        if(!CollectionUtils.isEmpty(map)){
            ret.getCommon().setRet(true);
        }

        return ret;
    }

    @GetMapping(value = "/withdrawSystem")
    @ResponseBody
    public Return queryWithdrawSystem(PageInfo info){
        Return ret=new Return();
        List<Map> map=payService.queryWithdrawSystem();
        ret.setData(map);
        if(!CollectionUtils.isEmpty(map)){
            ret.getCommon().setRet(true);
        }

        return ret;
    }



    @PostMapping("/withdraw")
    @ResponseBody
    public Return withdraw(@RequestHeader(value="token") String token,String withdrawAmount, String merchantId) {
        Return ret = new Return();
        if (payService.withdraw(token,withdrawAmount,merchantId, ret) > 0) {
            ret.getCommon().setRet(true);
        }
        return ret;
    }



}
